﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Studio
{
	// Token: 0x020004C4 RID: 1220
	public class GuideScale : GuideBase, IInitializePotentialDragHandler, IPointerDownHandler, IPointerUpHandler, IEventSystemHandler
	{
		// Token: 0x17000386 RID: 902
		// (get) Token: 0x06001F1B RID: 7963 RVA: 0x000C2308 File Offset: 0x000C0508
		private Camera camera
		{
			get
			{
				if (this.m_Camera == null)
				{
					this.m_Camera = Camera.main;
				}
				return this.m_Camera;
			}
		}

		// Token: 0x06001F1C RID: 7964 RVA: 0x000C232C File Offset: 0x000C052C
		public void OnPointerDown(PointerEventData eventData)
		{
		}

		// Token: 0x06001F1D RID: 7965 RVA: 0x000C2330 File Offset: 0x000C0530
		public void OnInitializePotentialDrag(PointerEventData _eventData)
		{
			this.prevPos = _eventData.position;
			this.dicChangeAmount = Singleton<GuideObjectManager>.Instance.selectObjectDictionary;
			this.dicOldScale = this.dicChangeAmount.ToDictionary((KeyValuePair<int, ChangeAmount> v) => v.Key, (KeyValuePair<int, ChangeAmount> v) => v.Value.scale);
		}

		// Token: 0x06001F1E RID: 7966 RVA: 0x000C23A4 File Offset: 0x000C05A4
		public override void OnDrag(PointerEventData _eventData)
		{
			base.OnDrag(_eventData);
			Vector3 b = Vector3.zero;
			if (this.axis == GuideScale.ScaleAxis.XYZ)
			{
				Vector2 delta = _eventData.delta;
				float d = (delta.x + delta.y) * this.speed;
				b = Vector3.one * d;
			}
			else
			{
				b = this.AxisPos(_eventData.position) - this.AxisPos(this.prevPos);
				this.prevPos = _eventData.position;
			}
			foreach (KeyValuePair<int, ChangeAmount> keyValuePair in this.dicChangeAmount)
			{
				Vector3 vector = keyValuePair.Value.scale;
				vector += b;
				vector.x = Mathf.Max(vector.x, 0.01f);
				vector.y = Mathf.Max(vector.y, 0.01f);
				vector.z = Mathf.Max(vector.z, 0.01f);
				keyValuePair.Value.scale = vector;
			}
		}

		// Token: 0x06001F1F RID: 7967 RVA: 0x000C24DC File Offset: 0x000C06DC
		public void OnPointerUp(PointerEventData _eventData)
		{
		}

		// Token: 0x06001F20 RID: 7968 RVA: 0x000C24E0 File Offset: 0x000C06E0
		public override void OnEndDrag(PointerEventData _eventData)
		{
			base.OnEndDrag(_eventData);
			GuideCommand.EqualsInfo[] changeAmountInfo = (from v in Singleton<GuideObjectManager>.Instance.selectObjectKey
			select new GuideCommand.EqualsInfo
			{
				dicKey = v,
				oldValue = this.dicOldScale[v],
				newValue = this.dicChangeAmount[v].scale
			}).ToArray<GuideCommand.EqualsInfo>();
			Singleton<UndoRedoManager>.Instance.Push(new GuideCommand.ScaleEqualsCommand(changeAmountInfo));
		}

		// Token: 0x06001F21 RID: 7969 RVA: 0x000C2528 File Offset: 0x000C0728
		private Vector3 AxisPos(Vector2 _screenPos)
		{
			Vector3 position = base.transform.position;
			Plane plane = new Plane(this.camera.transform.forward * -1f, position);
			Ray ray = RectTransformUtility.ScreenPointToRay(this.camera, _screenPos);
			float distance = 0f;
			Vector3 a = (!plane.Raycast(ray, out distance)) ? position : ray.GetPoint(distance);
			Vector3 vector = a - position;
			Vector3 onNormal = base.transform.up;
			GuideScale.ScaleAxis scaleAxis = this.axis;
			if (scaleAxis != GuideScale.ScaleAxis.X)
			{
				if (scaleAxis != GuideScale.ScaleAxis.Y)
				{
					if (scaleAxis == GuideScale.ScaleAxis.Z)
					{
						onNormal = Vector3.forward;
					}
				}
				else
				{
					onNormal = Vector3.up;
				}
			}
			else
			{
				onNormal = Vector3.right;
			}
			return Vector3.Project(vector, onNormal);
		}

		// Token: 0x040020F7 RID: 8439
		public GuideScale.ScaleAxis axis;

		// Token: 0x040020F8 RID: 8440
		private float speed = 0.001f;

		// Token: 0x040020F9 RID: 8441
		private Vector2 prevPos = Vector2.zero;

		// Token: 0x040020FA RID: 8442
		private Camera m_Camera;

		// Token: 0x040020FB RID: 8443
		private Dictionary<int, Vector3> dicOldScale;

		// Token: 0x040020FC RID: 8444
		private Dictionary<int, ChangeAmount> dicChangeAmount;

		// Token: 0x020004C5 RID: 1221
		public enum ScaleAxis
		{
			// Token: 0x04002100 RID: 8448
			X,
			// Token: 0x04002101 RID: 8449
			Y,
			// Token: 0x04002102 RID: 8450
			Z,
			// Token: 0x04002103 RID: 8451
			XYZ
		}
	}
}
